package org.javastudy.tang.lambda.predicate;

import org.javastudy.tang.vo.User;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p><em>Created on 2022/6/27 9:08 上午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
public class PredicateTest2 {

    public static void main(String[] args) {

        User user1 = new User("张三", "女", 1);
        User user2 = new User("李四", "男", 2);
        User user3 = new User("张三", "女", 3);
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);

        /**
         * 1、获取年龄大于2的对象
         */
        List<User> collect = list.stream().filter(x -> x.getAge() > 2).collect(Collectors.toList());
        System.out.println("获取年龄大于2的数量 = " + collect.size());
        //输出：获取年龄大于2的数量 = 1

        /**
         * 2、去重 设置name相同即为相同对象
         */
        //方式1直接使用 distinct
        List<User> collect1 = list.stream().distinct().collect(Collectors.toList());
        System.out.println("输出剩余对象" + collect1);
        //输出：输出剩余对象[User(name=张三, sex=女, age=1), User(name=李四, sex=男, age=2)]

        /**
         * 3、从集合找出与该对象相同的元素 同样name相同即为相同对象
         */
        User user4 = new User("张三", "男", 8);
        Predicate<User> predicate =  Predicate.isEqual(user4);
        List<User> collect2 = list.stream().filter(predicate).collect(Collectors.toList());
        System.out.println("与该对象相同的对象有" + collect2);
        //输出：与该对象相同的对象有[User(name=张三, sex=女, age=1), User(name=张三, sex=女, age=3)]

    }

}
